package com.example.reagent_management.ui.setting

import android.content.Context
import android.view.View
import com.example.reagent_management.R
import kotlinx.coroutines.DelicateCoroutinesApi
import java.lang.ref.WeakReference

class UserLogTool {

    companion object {

        /**
         *
         * 功能描述: 控件点击log记录
         *
         * @param: [context 上下文（这个必须要传过来，不能直接使用MyApplication.getAppContext），控件页面tag， id 控件id， fileName log文件名]
         * @return:
         * @author: huangtao
         * @date: 2022/2/23 16:36
         */
        @DelicateCoroutinesApi
        //context 不是app级别，建议弱引用持有，如果context突然被回收掉,但伴生对象还持有,context释放不了??
        fun writeLogToFile(context: WeakReference<Context>, tag: String, view: Any, fileName: String) {
            context.get()?.let {
                var pageName = ""
                var buttonName = ""
                when (tag) {
                    //首页
                    "HomeFragment" -> pageName = "首页"
                    //设置
                    "SettingFragment" -> pageName = "设置"
                    "BioOverViewFragmentKt" -> pageName = "生化-试剂概览"
                    "MsgErrDlgFragment" -> pageName = "错误"
                    "MsgWarnDlgFragment" -> pageName = "警告"
                    "BioDetailFragment" -> pageName = "生化-试剂详情"
                    "TestFragment" -> pageName = "测试"
                    else -> pageName = tag
                }
                buttonName = if (view is View){
                    when (view.id) {
                        R.id.btn_setting -> "设置"
                        else -> view.toString()
                    }
                }else{
                    view.toString()
                }

                buttonName = "------$buttonName------Clicked"

                LogUtils.log(pageName,buttonName,fileName)

                //启用全局协程进行io写入 主线程退出，其协程也会结束
//                GlobalScope.launch(Dispatchers.IO) {
//                    Logger.writeToFile(pageName, buttonName, fileName)
//                }
            }
        }
    }
}